package org.mtmi.ui.controls;

import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.mtmi.ui.M;
import org.mtmi.ui.drawables.IDrawable;
import org.mtmi.ui.layouts.LayoutData;
import org.mtmi.ui.scenes.COLOR;
import org.mtmi.ui.scenes.FontManager;
import org.mtmi.ui.scenes.Scene;

/**
 * A label displaying text and/or image (text on the left of image)
 *
 */
public class Label extends Control
{
	private static final int SP=5;
	
	protected String text;
	protected Font   font;
	protected IDrawable image;
	
	public Label( Scene scene, int _flags ) {
		this(scene, null, null, _flags);
	}
	
	public Label(Scene scene, String _text, int _flags) {
		this( scene, _text, null, _flags );
	}
	
	public Label( Scene scene, String _text, IDrawable _img, int _flags ) {
		super(scene, _flags);
		text = _text;
		image = _img;
		font = FontManager.getInstance().getNormalFont();
		setForeground( COLOR.grey100 );
	}

	public Font getFont() { return font; }
	
	/** change label's font, caller is responsible to dispose font */
	public void setFont( Font f ) { font=f; }
	
	@Override
	public Point computeSize( LayoutData l )
	{
		Point st=null;
		int w =0;
		int hx = 0;
		if( text!=null && !text.isEmpty() ) {
			l.gc.setFont( font );
			st = l.gc.textExtent( text );
			w  = st.x;
			hx = st.y; 
		}
		Point si=null;
		if( image!=null ) {
			si = image.computeSize( 0, 0 );
			if( w>0 ) w+=SP;
			w += si.x;
			hx = Math.max( hx, si.y );
		}
		//defualt values:
		if( w == 0 ) w=10;
		if( hx==0 ) hx=10;
		return new Point( w, hx );
	}
	
	public String getText() { return text; }
	
	/** label is not resized or redrawn, if needed caller should do */
	public void setText( String t ) { 
		text=t; 
	}
	
	public IDrawable getImage() { return image; }
	
	public void setImage( IDrawable d ) {
		image=d;
	}

	@Override
	public void paint( GC gc )
	{
		//nothing to paint ?
		if( (text==null || text.isEmpty()) && image==null ) return ;
		
		gc.setForeground( getForeground() );
		gc.setFont( getFont() );
		
		boolean top = (getFlags() & M.TOP)!=0;
		boolean btm = (getFlags() & M.BOTTOM)!=0;
		boolean lft = (getFlags() & M.LEFT)!=0;
		boolean rgt = (getFlags() & M.RIGHT)!=0;
		
		//like computeSize(), required to align image and text
		Point st = null;
		Point si = null;
		int w = 0;
		int hx = 0;
		if( text!=null && !text.isEmpty() ) {
			st = gc.textExtent( text );
			w = st.x;
			hx = st.y;
		}
		if( image!=null ) {
			si = image.computeSize( 0, 0 );
			if( w>0 ) w+=SP;
			w += si.x;
			hx = Math.max( hx, si.y );
		}
		
		int x,y;
		if( lft && rgt ) { //hcenter
			x = location.x + (size.x-w)/2;
		}
		else if( rgt ) {
			x = location.x + size.x - w;
		}
		else { //lft, by default
			x = location.x;
		}

		if( top && btm ) { //vcenter
			y = location.y + (size.y-hx)/2;
		}
		else if( rgt ) {
			y = location.y + size.y - hx;
		}
		else { //top, by default
			y = location.y;
		}

		if( si!=null && image!=null ) {
			int yi = y+(hx-si.y)/2;
			image.paint( gc, x, yi, si.x, si.y, IDrawable.NORMAL );
			x += si.x + SP;
		}
		if( st!=null ) {
			int yt = y+(hx-st.y)/2;
			gc.drawText( text, x, yt, true );
		}
		
	}

}
